package com.collectplus.express.logic;

import java.util.ArrayList;
import java.util.HashMap;

import droid.frame.utils.lang.JsonParser;

/**
 * 
 * @author mark<br>
 *         coffee modify in time 2015-6-10下午8:24:05<br>
 *         主要修改非空判断。以及return确保为空
 */
public class JSON {

	/**
	 * java.lang.ClassNotFoundException: T
	 * 
	 * @param jsonStr
	 * @return
	 */
	public static AppResult<?> parser(Object jsonStr) {
		if (jsonStr == null) {
			return new AppResult<Object>();
		}
		AppResult<?> reqResult = JsonParser.parse(jsonStr + "", AppResult.class);
		if (reqResult == null) {
			reqResult = new AppResult<Object>();
		}
		return reqResult;
	}

	@SuppressWarnings("unchecked")
	public static <T> AppResult<T> parser(Object jsonStr, Class<T> resultType) {
		if (jsonStr == null) {
			return new AppResult<T>();
		}
		AppResult<T> reqResult = JsonParser.parse(jsonStr + "", AppResult.class);
		if (reqResult == null) {
			return new AppResult<T>();
		} else {
			T obj = JsonParser.parse(jsonStr + "", "result", resultType);
			reqResult.setResult(obj);
			return reqResult;
		}
	}

	@SuppressWarnings("unchecked")
	public static <T> AppResult<T> parseList(Object jsonStr, Class<T> resultType) {
		if (jsonStr == null) {
			return new AppResult<T>();
		}
		AppResult<T> reqResult = JsonParser.parse(jsonStr + "", AppResult.class);
		if (reqResult == null) {
			return new AppResult<T>();
		} else {
			ArrayList<T> list = JsonParser.parseList(jsonStr + "", "result", resultType);
			reqResult.setResultList(list);
			return reqResult;
		}
	}

	@SuppressWarnings("unchecked")
	public static <T> AppResult<T> parseMap(Object jsonStr, Class<T> valueTpye) {
		if (jsonStr == null) {
			return new AppResult<T>();
		}
		AppResult<T> reqResult = JsonParser.parse(jsonStr + "", AppResult.class);
		if (reqResult == null) {
			return new AppResult<T>();
		} else {
			HashMap<String, T> map = JsonParser.parseMap(jsonStr + "", "result", valueTpye);
			reqResult.setResultMap(map);
			return reqResult;
		}
	}
}
